
package com.probiz.estore.product.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.ArrayUtils;

import com.probiz.estore.attribute.service.AttributeManager;
import com.probiz.estore.common.model.product.Brand;
import com.probiz.estore.common.model.product.PriceInterval;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.common.model.product.SkuOption;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductTypeBrandDao;
import com.probiz.estore.product.dao.ProductTypeDao;
import com.probiz.estore.product.dao.ProductTypeSkuOptionDao;
import com.probiz.estore.product.model.ProductTypeFullyModel;
import com.probiz.estore.product.service.ProductAttGroupItemManager;
import com.probiz.estore.product.service.ProductAttGroupManager;
import com.probiz.estore.product.service.ProductTypeManager;

/**
 * Manager implementation for ProductType, responsible for business processing,
 * and communicate between web and persistence layer.
 */
public class ProductTypeManagerImpl extends GenericManagerImpl<ProductType>
		implements ProductTypeManager {
	private ProductAttGroupManager		productAttGroupManager		= null;
	private ProductAttGroupItemManager	productAttGroupItemManager	= null;
	private AttributeManager  attributeManager = null;
	private ProductTypeDao				productTypeDao				= null;
	private ProductTypeBrandDao productTypeBrandDao = null;
	private ProductTypeSkuOptionDao productTypeSkuOptionDao = null;


	
	public void setProductTypeDao(ProductTypeDao productTypeDao) {
		this.productTypeDao = productTypeDao;
	}


	public void setProductTypeBrandDao(ProductTypeBrandDao productTypeBrandDao) {
		this.productTypeBrandDao = productTypeBrandDao;
	}
	
	

	public void setProductTypeSkuOptionDao(
			ProductTypeSkuOptionDao productTypeSkuOptionDao) {
		this.productTypeSkuOptionDao = productTypeSkuOptionDao;
	}
	
	public void setProductAttGroupManager(ProductAttGroupManager productAttGroupManager) {
		this.productAttGroupManager = productAttGroupManager;
	}

	public void setProductAttGroupItemManager(ProductAttGroupItemManager productAttGroupItemManager) {
		this.productAttGroupItemManager = productAttGroupItemManager;
	}

	
	public void setAttributeManager(AttributeManager attributeManager) {
		this.attributeManager = attributeManager;
	}


	@Override
	protected void initManager() {
		dao = productTypeDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductType entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#preSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductType entity) {

	}
	
	

	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.catalog.service.ProductTypeManager#getPriceIntervalsByProductType(com.probiz.estore.common.model.catalog.ProductType)
	 */
	public List<PriceInterval> getPriceIntervalsByProductType(ProductType productType) {
		List<PriceInterval> result = null;

		if (productType != null) {
			result = productType.getPriceIntervalList();
			
			if (result == null || result.isEmpty()) {
				//系统自动计算
				//Maximum price
				int maxPrice = productTypeDao.getMaximumPriceByProductType(productType.getProductTypeId());
				
				//divide interval
				if (maxPrice > 0) {
					result = new ArrayList<PriceInterval>();
				}
				if (500 >= maxPrice && maxPrice > 0) {
					result.add(new PriceInterval("(,100)"));
					result.add(new PriceInterval("[100,200]"));
					result.add(new PriceInterval("[200,300]"));
					result.add(new PriceInterval("[300,]"));
				} else
				if (1000 > maxPrice &&  maxPrice > 500) {
					result.add(new PriceInterval("(,200)"));
					result.add(new PriceInterval("[200,500]"));
					result.add(new PriceInterval("[500,)"));
				} else 
				if (2000 > maxPrice && maxPrice > 1000) {
					result.add(new PriceInterval("(,500)"));
					result.add(new PriceInterval("[500,1000)"));
					result.add(new PriceInterval("[1000,)"));
				} else
				if (5000 > maxPrice && maxPrice > 2000) {
					result.add(new PriceInterval("(,500)"));
					result.add(new PriceInterval("[500,1000)"));
					result.add(new PriceInterval("[1000,1500)"));
					result.add(new PriceInterval("[1500,2000)"));
					result.add(new PriceInterval("[2000,)"));
				} else
				if (10000 > maxPrice && maxPrice > 5000) {
					result.add(new PriceInterval("(,1000)"));
					result.add(new PriceInterval("[1000,2000)"));
					result.add(new PriceInterval("[2000,3000)"));
					result.add(new PriceInterval("[3000,4000)"));
					result.add(new PriceInterval("[4000,5000)"));
					result.add(new PriceInterval("[5000,)"));	
				} else
				if (20000 > maxPrice && maxPrice > 10000) {
					result.add(new PriceInterval("(,2000)"));
					result.add(new PriceInterval("[2000,5000)"));
					result.add(new PriceInterval("[5000,8000)"));
					result.add(new PriceInterval("[8000,10000)"));
					result.add(new PriceInterval("[10000,)"));
				} else
				if (50000 > maxPrice && maxPrice > 20000) {
					result.add(new PriceInterval("(,5000)"));
					result.add(new PriceInterval("[5000,8000)"));
					result.add(new PriceInterval("[8000,10000)"));
					result.add(new PriceInterval("[10000,15000)"));
					result.add(new PriceInterval("[15000,18000)"));
					result.add(new PriceInterval("[18000,20000)"));
					result.add(new PriceInterval("[20000,)"));
				} else
				if (maxPrice > 50000) {
					
					result.add(new PriceInterval("(,20000)"));
					result.add(new PriceInterval("[20000,30000)"));
					result.add(new PriceInterval("[30000,40000)"));
					result.add(new PriceInterval("[40000,50000)"));
					result.add(new PriceInterval("[50000,)"));
				}
			}
		}
		return result;
	}
	
	/**
	 * 获得产品类别价格的最大值
	 * @param productTypeId
	 * @return
	 */
	public int getMaximumPriceByProductType(Integer productTypeId) {
		return productTypeDao.getMaximumPriceByProductType(productTypeId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductTypeManager#saveFully(com.probiz.estore.product.model.ProductTypeFullyModel)
	 */
	public Map<String, Object> saveFully(ProductTypeFullyModel model){
		Map<String, Object> returnMsgs = new HashMap<String, Object>();
        ProductType productType = model.getProductType();
        save(productType);
        productTypeBrandDao.saveProductTypeBrandsFromArrays(productType, model.getRelatedBrandIds());
        productTypeSkuOptionDao.saveProductTypeSkuOptionsFromArray(productType,model.getRelatedSkuOptionIds(),model.getSkuOptionIsQuickSearchs());
        
     // start:属性组保存
		List<String> newGroupIds = productAttGroupManager.saveGroupsFromArrays(
				productType, model.getGroupIds(), model.getGroupNames(),
				model.getGroupSortOrders());
		returnMsgs.put("newGroupIds", newGroupIds);
		// end:属性组保存

		// -----import
		String[] attrItemGroupIds = getNewAttrItemGroupIds(model
				.getOldGroupIds(), newGroupIds, model.getOldAttrItemGroupIds());
		// start:保存attrItem信息
		List<String> newAttrItemIds = productAttGroupItemManager
				.saveAttrItemsFromArrays(productType, attrItemGroupIds,
						model.getAttrItemIds(), model.getAttrItemSortOrders(),
						model.getAttrItemIsQuickSearchs(), model
								.getAttrItemShowStyles(), model
								.getAttrItemSearchValues(), model
								.getAttrItemAttributeIds());
		returnMsgs.put("newAttrItemIds", newAttrItemIds);
		returnMsgs.put("newAttrItemGroupIds", attrItemGroupIds);
		// end:保存attrItem信息

		// start:将多余的attribute删除掉

		for (String attributeId : model.getDeleteAttributeIds()) {
			attributeManager.deleteById(Integer.parseInt(attributeId));
		}
		// end:将多余的attribute删除掉
        return returnMsgs;
		
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductTypeManager#deleteFully(java.lang.Integer)
	 */
	public short deleteFully(Integer productTypeId){
		Long productCount = productTypeDao.getProductCountByProductType(productTypeId);
		if(productCount.longValue() == (long)0){
			productTypeDao.delete(productTypeDao.getById(productTypeId));
			return DELETE_STATUS_TRUE;
		}else{
			return DELETE_STATUS_FALSE;
		}
	}
	
	/**
	 * 根据oldGroupIds 、 newAttGroupIds以及oldAttrItemIds 生成newAttItemGroupIds
	 * @param oldGroupIds
	 * @param newAttGroupIds
	 * @param oldAttrItemGroupIds
	 * @return
	 */
	private String[] getNewAttrItemGroupIds(String[] oldGroupIds,
			List<String> newAttGroupIds, String[] oldAttrItemGroupIds) {
		String[] newAttItemGroupIds = (String[]) ArrayUtils
				.clone(oldAttrItemGroupIds);
		for (int i = 0; i < oldAttrItemGroupIds.length; i++) {
			String oldGroupId = oldAttrItemGroupIds[i].substring(2);
			int index = ArrayUtils.indexOf(oldGroupIds, oldGroupId);
			String newGroupId = newAttGroupIds.get(index);
			newAttItemGroupIds[i] = newGroupId;
		}

		return newAttItemGroupIds;
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductTypeManager#getRelatedBrand(java.lang.Integer)
	 */
    public List<Brand> getRelatedBrand(Integer productTypeId){
    	return productTypeDao.getRelatedBrand(productTypeId);
    }
    /*
     * (non-Javadoc)
     * @see com.probiz.estore.product.service.ProductTypeManager#getRelatedSkuOption(java.lang.Integer)
     */
    public List<SkuOption> getRelatedSkuOption(Integer productTypeId){
    	return productTypeDao.getRelatedSkuOption(productTypeId);
    }
    
    public ProductType getProductTypeByName(String productTypeName){
    	return productTypeDao.getProductTypeByName(productTypeName);
    }

	public boolean getIsProductTypeExist(String productTypeName) {
		return this.productTypeDao.isProductTypeExist(productTypeName);
	}

	public boolean getIsProductTypeNameUseByOthers(String productTypeName,
			Integer productTypeId) {
		return this.productTypeDao.isProductTypeNameUseByOthers(productTypeName, productTypeId);
	}
}
